<?php
class CmsProfilePhotoEditControl extends CmsBaseControl
{
    protected $userId;

    public function CmsProfilePhotoEditControl()
    {
        parent::__construct();
        $this->userId = $this->config->GetUserModel()->GetCurrentUserId();
    }

    public function Render()
    {
        $photo = $this->config->GetUserModel()->FindScalarById($this->userId, 'profile_photo');
        $photo = Photo::GetInstance()->GetMediumUrl($photo);

        $uploadUrl = $this->GetRefreshUrl();
        $upload = new UploadControl($uploadUrl);
        $upload->AddParam('action', 'PhotoUpload');

        $this->Replace('upload', $upload->Render());
        $this->Replace('photo', $photo);
        return parent::Render();
    }

    public function PhotoUpload()
    {
        $user['profile_photo'] = Photo::GetInstance()->SaveUploadedPhoto();
        $user['id'] = $this->userId;
        $this->config->GetUserModel()->Update($user);
    }
}
?>
